<?php


namespace app\lunwen\controller;

use app\lunwen\model\StudentModel;
use app\lunwen\model\StuTopicModel;
use app\lunwen\service\ExcelService;
use cmf\controller\HomeBaseController;

class PpController extends HomeBaseController
{
    public  function index(){
        return $this->fetch(':pp');
    }

    public function readtest(){
        $excel = new ExcelService();
        $data = $excel->importData(CMF_ROOT.'/public/upload/1.xlsx');
        $sql_column = ['stu_name','stu_title','status','create_time','stu_id','stu_teacher'];

        // 如果正常读取，则遍历并转换一些数据（数组键、时间戳）
        $total_array = [];
        foreach ($data as $key => $item1){
            $key_array = [];
            // 对数组键进行转换
            for($i=0; $i<count($sql_column); $i++){
                $key_array[$sql_column[$i]] = $item1[$i];
            }
            // 将转换完成的数据打包
            array_push($total_array,$key_array);
        }

        $model = new StuTopicModel();
        $count = $model->limit(100)->insertAll($total_array);
        return '成功导入'.$count.'条数据';
    }

    public function writetest(){
        $excel = new ExcelService();
        $model = new StuTopicModel();
        $data = $model->field('stu_name,stu_title,status,create_time,stu_id,stu_teacher')
                      ->select()->toArray(array());
        $head = ['学生姓名','学生课题','状态','创建时间','学生号','学生导师'];
        $excel->outData('测试',$head,[],$data);
    }

}